<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                  |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                     |
// +------------------------------------------------------------------------+
// | Author: liuxuliang@clcw.com.cn                                           |
// +------------------------------------------------------------------------+
//
/**
 * 车主争议
 */

namespace AuctionBack\Controller;

use AuctionCore\System\AuctionException;
use AuctionCore\AuctionOrder\AuOrderList as AuOrderList;
use AuctionCore\AuctionOrder\AuOrder;
use AuctionCore\Issue\OwnerIssue;
use AuctionCore\CarResource\Car;
use AuctionCore\CarOwner\Owner;
use AuctionCore\Account\Back\Employee;
use AuctionCore\Other\Address;
use AuctionCore\CarDealer\AssetCenter;
use AuctionCore\Other\Inbox;

class IssueOwnerController extends BaseController {

    const PAGE_NUM = 1; // 默认起始页
    const PAGE_SIZE = 10; // 每页偏移量

    private $dataStatus;

    public function __construct() {
        $this->dataStatus = OwnerIssue::$dataStatus;
        parent::__construct();
        $this->emp_id = $this->user_info['emp_id'];
    }

    /**
     * 车主争议模块主页面
     */
    public function index() {
        $status = I("get.status/d", OwnerIssue::STATUS_PENDING);
        $params = array(
            'page' => I("get.p", self::PAGE_NUM),
            'size' => self::PAGE_SIZE,
            'search' => array(
                    'status' => $status,
                )
        );
        $show_from = \AuctionCore\Other\Channel::get_from();  //获取渠道来源信息
        $this->assign('show_from', $show_from);
        //获取父级渠道来源
        $pfrom = \AuctionCore\Other\Channel::get_parent_channel_list();
        $this->assign('pfrom', $pfrom);
        //获取渠道来源列表
        $from = \AuctionCore\Other\Channel::get_child_channel_list();
        $this->assign('from', $from);
        $content = $this->getPendingList($params);
        $this->assign('params', $params);
        $this->assign('content', $content);

        $comefrom_list = \AuctionCore\Other\Channel::get_comefrom_list();
        //城市对应的4s店
        $bc_city = $this->bc_city;
        $city_fours_list = \AuctionCore\Other\Channel::get_city_fours_list($bc_city);
        $this->assign('comefrom_list',$comefrom_list);
        $this->assign('city_fours_list',$city_fours_list);
        $this->display();
    }

    /**
     * 数据列表
     */
    public function getList() {
        //获取父级渠道来源
        $pfrom = \AuctionCore\Other\Channel::get_parent_channel_list();
        $this->assign('pfrom', $pfrom);
        $show_from = \AuctionCore\Other\Channel::get_from();  //获取渠道来源信息
        $this->assign('show_from', $show_from);
        $status = I("get.status/d", OwnerIssue::STATUS_PENDING);
        $params = array(
            'page' => I("get.p", self::PAGE_NUM),
            'size' => self::PAGE_SIZE,
            'search' => array(
                    'status' => $status,
                    'location_area' => I('get.location_area', ''),
                    'comeFrom' => I('get.comefrom', '-2'),
                    'order_no' => I('get.order_no', ''),
                )
        );
        if (!in_array($status, $this->dataStatus)) {
            $this->showError('参数错误，请联系管理员');
        }
        $this->getPendingList($params, 1);
        // dump($content);
        // $this->ajaxReturn($content);
    }

    /**
     * 错误输出
     *
     * 页面片段
     * 
     * @param  string $errMsg [description]
     * @return [type]         [description]
     */
    public function showError($errMsg = '') {
        $this->assign('errMsg', $errMsg);
    }

    /**
     * 争议数据列表
     */
    private function getPendingList($params, $isDisplay=0) {
        if(!$this->is_hq && $this->bc_city){
           $params['search']['location_area'] = array('in',array_flip($this->bc_city));
        }
        //获取list
        $aol = new OwnerIssue();
        $result = $aol->issue_owner_list($params['page'], $params['size'], $params['search']);
        // dump($result);
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $params['size'], "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $page = $Page->show(); // 分页显示输出
        //模板赋值
        $this->assign('page', $page); // 赋值分页输出
        $this->assign('list', $result['list']);
        $this->assign("params", $params);
        if ($isDisplay == 1) {
            $this->display('pendingList');
        } else {
            return $this->fetch('pendingList');
        }
    }

    /**
     * 车主争议处理详情页
     */
    public function deal() {
        //------获取数据
        $order_id = I('get.id');
        $ownerIssue = new OwnerIssue($order_id);
        $detail = $ownerIssue->issue_owner_detail();
        $car_detail = \AuctionCore\CarResource\Car::getDetailWithChannel($detail['car_id']);                            
        $this->assign('car_detail', $car_detail);
        $show_from = \AuctionCore\Other\Channel::get_from();  //获取渠道来源信息
        $this->assign('show_from', $show_from);

        $transfer_type = C('TRANSFER_LIMIT');
        $this->assign('transfer_type', $transfer_type);

        //获取提车人信息
        $take_car_men = \AuctionCore\CarDealer\Dealer::getTakeCarMan($detail['dealer_id']);
        $this->assign('detail', $detail);
        $this->assign('dataStatus', $this->dataStatus);
        $this->assign('transferType', $this->transferType);
        $this->assign('take_men', $take_car_men);
        $this->display();
    }

    /**
     * 车主争议详情展示页
     */
    public function view() {
        //------获取数据
        $order_id = I('get.id');
        $ownerIssue = new OwnerIssue($order_id);
        $detail = $ownerIssue->issue_owner_detail();    
        $car_detail = \AuctionCore\CarResource\Car::getDetailWithChannel($detail['car_id']);

        $this->assign('car_detail', $car_detail);

        $show_from = \AuctionCore\Other\Channel::get_from();  //获取渠道来源信息
        $this->assign('show_from', $show_from);

        $transfer_type = C('TRANSFER_LIMIT');
        $this->assign('transfer_type', $transfer_type);

        //获取提车人信息
        $take_car_men = \AuctionCore\CarDealer\Dealer::getTakeCarMan($detail['dealer_id']);
        $this->assign('detail', $detail);
        $this->assign('dataStatus', $this->dataStatus);
        $this->assign('transferType', $this->transferType);
        $this->assign('take_men', $take_car_men);
        $this->display();
    }

    /**
     * 验证拍单编号是否有效
     *
     * @param int $orderID
     * @return fix
     */
    private function checkConsultOrderByID($orderID='') 
    {
        // 判断ID格式是否正确
        if (empty($orderID) || !is_numeric($orderID)) {
            $this->helpJsonResult(400,'当前操作拍单编号异常!');
        }
        // 判断ID是否有效
        $o = new AuOrder($orderID);
        $orderInfo = $o->detail();
        if (!is_array($orderInfo) || empty($orderInfo['order_no'])) {
            $this->helpJsonResult(400,'当前操作拍单编号无效!');
        }
        return true;
    }

    /**
     * 协商补偿处理
     * 
     * @return json
     */
    public function doConsultPay()
    {
        if (!IS_POST) $this->helpJsonResult(500,"当前请求方式不合法！");
        $payPrice = I('post.payprice');
        if (!is_numeric($payPrice) || $payPrice < 0) {
            $this->helpJsonResult(400,'车主降价价格异常!');
        }
        // 验证拍单ID是否有效
        $orderID = I('post.order_id'); // 拍单ID
        $this->checkConsultOrderByID($orderID);
        $params = array();
        // 获取表单信息
        $params['issue_id'] = I('post.issue_id'); // 车商争议ID
        $params['order_id'] = $orderID;
        $params['emp_id'] = $this->emp_id;
        $params['status'] = 1; // 争议处理结果(0:待处理, 1:协商成功 2:车主不卖)
        $params['cut_down_price'] = I('post.payprice'); // 车主降价金额
        $params['remark'] = I('post.pay_remark', '', 'htmlspecialchars'); // 备注
        try {
            $res = \AuctionCore\Issue\OwnerIssue::save_owner_issue($params);
        } catch (AuctionException $ex) {
            $this->helpJsonResult(500, $ex->getMessage());
        }
        if ($res) {
            $this->helpJsonResult(200, '更新成功');
        } else {
            $this->helpJsonResult(500, '协商处理失败！');   
        }
    }

    /**
     * 车商违约处理
     * 
     * @return json
     */
    public function doVialate()
    {
        if (!IS_POST) $this->helpJsonResult(500,"当前请求方式不合法！");
        // 验证拍单ID是否有效
        $orderID = I('post.order_id'); // 拍单ID
        $this->checkConsultOrderByID($orderID);
        $params = array();
        // 获取表单信息
        $params['issue_id'] = I('post.issue_id'); // 车商争议ID
        $params['car_id'] = I('post.car_id');
        $params['order_id'] = $orderID;
        $params['emp_id'] = $this->emp_id;
        $params['status'] = 2; // 争议处理结果(0:待处理, 1:协商成功 2:车主不卖)
        $params['remark'] = I('post.violate_remark', '', 'htmlspecialchars'); // 协商补偿备注
        try {
            $res = \AuctionCore\Issue\OwnerIssue::save_owner_issue($params);
        } catch (AuctionException $ex) {
            $this->helpJsonResult(500, $ex->getMessage());
        }
        if ($res) {
            $this->helpJsonResult(200, '更新成功');
        } else {
            $this->helpJsonResult(500, '违约处理失败！');   
        }
    }

}
